package com.uenpay.oem.ui.bankcard;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.uenpay.im.utilities.Toasts;
import com.uenpay.im.weight.dialog.LoadDialog;
import com.uenpay.oem.MyApplication;
import com.uenpay.oem.R;
import com.uenpay.oem.adapter.BankCardAdapter;
import com.uenpay.oem.data.bean.UserAuthInfo;
import com.uenpay.oem.ui.auth.AuthActivity;
import com.uenpay.oem.ui.reserver.AddnewcardActivity;
import com.uenpay.oem.ui.webview.WebviewActivity;
import com.uenpay.res.widget.dialog.WhiteBgDialog;
import com.uenpay.service.bean.BankCardInfo;
import com.uenpay.util.app.base.BaseActivity;
import com.uenpay.util.common.DensityUtils;
import com.uenpay.util.widget.sliderecyclerview.Closeable;
import com.uenpay.util.widget.sliderecyclerview.OnSwipeMenuItemClickListener;
import com.uenpay.util.widget.sliderecyclerview.SwipeMenu;
import com.uenpay.util.widget.sliderecyclerview.SwipeMenuCreator;
import com.uenpay.util.widget.sliderecyclerview.SwipeMenuItem;
import com.uenpay.util.widget.sliderecyclerview.SwipeMenuRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class MyBankCardActivity extends BaseActivity implements MyBankCardContract.View, View.OnClickListener {
    MyBankCardPresenter mPresenter;
    private BankCardAdapter adapter;

    @BindView(R.id.btn_add_card)
    Button btnAddCards;
    @BindView(R.id.ll_no_card_default_layout)
    LinearLayout llDefaultNoCard;
    private WhiteBgDialog dialog;
    @BindView(R.id.slide_list)
    SwipeMenuRecyclerView listView;
    public List<BankCardInfo> data;

    private Context mContext;
    private OnSwipeMenuItemClickListener menuItemClickListener = new OnSwipeMenuItemClickListener() {

        @Override
        public void onItemClick(Closeable closeable, int adapterPosition, int menuPosition, @SwipeMenuRecyclerView.DirectionMode int direction) {
            closeable.smoothCloseMenu();
            switch (menuPosition) {
                case 0:
                    mPresenter.setDefaultcard(adapterPosition);
                    break;
                case 1:
                    mPresenter.unbindcard(adapterPosition);
                    break;
            }
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mContext = this;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_my_bank_card;
    }

    @Override
    public void initView(View view) {
        initView();
        setPresenter(null);
        //initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        mPresenter.loadCards();
    }

    private void initView() {
        data = new ArrayList<>();
        llDefaultNoCard.setVisibility(View.GONE);
        initRecyclerView();
        setTitle("我的银行卡");
        setRightBtnBackGround(R.drawable.icon_bankhelp);

    }

    private void initRecyclerView() {
        listView.setLayoutManager(new LinearLayoutManager(mContext));
        listView.setSwipeMenuCreator(creator);
        listView.setSwipeMenuItemClickListener(menuItemClickListener);
        adapter = new BankCardAdapter(this, data);
        listView.setAdapter(adapter);
    }

    @OnClick(R.id.btn_add_card)
    public void addCard(View view){
        UserAuthInfo userAuthInfo = MyApplication.getUserAuthInfo();
        if(null == userAuthInfo){
            Toasts.shortToast(this,"未获取用户信息");
            return ;
        }
        int isReal =  userAuthInfo.getIsReal();
        if(1 == isReal){// 0 未认证 1 已认证
            startActivity(new Intent(this,AddnewcardActivity.class));
        } else {
            showDialog();
        }

    }

    @Override
    public void onRightBtnInTopBarClick() {
        goHelp();
    }

    public void goHelp(){
        startActivity(new Intent(this, WebviewActivity.class));
    }


    @Override
    public void setPresenter(Object presenter) {
        mPresenter = new MyBankCardPresenter(this);
    }

    @Override
    public void showToast(String msg) {
        Toasts.shortToast(this,msg);
    }

    @Override
    public void showLoading() {
        LoadDialog.show(this);
    }

    @Override
    public void dismissLoading() {
        LoadDialog.dismiss(this);
    }

    @Override
    public void completeLoad() {
        if (data.size()>0){
            llDefaultNoCard.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
        } else {
            llDefaultNoCard.setVisibility(View.VISIBLE);
            listView.setVisibility(View.GONE);
        }
        adapter.notifyDataSetChanged();//刷新数据
    }

    @Override
    public void setData(List<BankCardInfo> data) {
        this.data.clear();
        this.data.addAll(data);
    }

    private void showDialog(){
        dialog = new WhiteBgDialog(this, R.layout.dialog_real_name_certification);
        dialog.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_left:
                startActivity(new Intent(this, AuthActivity.class));//// TODO: 2017/6/18 实名认证
                dialog.dissmiss();
                break;
            case R.id.btn_right:
                dialog.dissmiss();
                //startActivity(new Intent(this, AddnewcardActivity.class));//添加银行卡
                break;
        }
    }

    /**
     * item右侧菜单
     */
    private SwipeMenuCreator creator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
            int width1 = DensityUtils.dip2px(100);
            int width2 = DensityUtils.dip2px(60);
            int height = DensityUtils.dip2px(102);
            int textSize = DensityUtils.sp2px(6);

            SwipeMenuItem setDefaultMenu = new SwipeMenuItem(mContext)
                    .setBackgroundColor(0xff525864)
                    .setText(R.string.txt_set_default)
                    .setTextColor(0xffffffff)
                    .setWidth(width1)
                    .setTextSize(textSize)
                    .setHeight(height);
            SwipeMenuItem unbindMenu = new SwipeMenuItem(mContext)
                    .setBackgroundDrawable(R.drawable.round_right_corner_grey_bg)
                    .setText(R.string.txt_unbind)
                    .setTextColor(0xffffffff)
                    .setTextSize(textSize)
                    .setWidth(width2)
                    .setHeight(height);

            swipeRightMenu.addMenuItem(setDefaultMenu);
            swipeRightMenu.addMenuItem(unbindMenu);


        }
    };
}
